package com.blink.curator.operate;

import com.blink.base.BaseTestSample;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.zookeeper.CreateMode;
import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

/**
 * <Description> 创建节点</Description>
 * <ClassName> CreateNodeSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 14:38
 */
public class CreateNodeSample extends BaseTestSample {
    /**
     * <Title>testCreateNode</Title>
     * <Description> 创建节点</Description>
     *
     * @return void
     * @throws
     */
    @Test
    public void testCreateNode() throws Exception {
        CuratorFramework client = getZkCurator();
        client.start();
        String path = "/zk-book/c1";
        client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .forPath(path, "init".getBytes());
        sleep();
    }

    /**
     * <Title>testCreateNodeBackground</Title>
     * <Description> TODO</Description>
     *
     * @return void
     * @throws
     */
    @Test
    public void testCreateNodeBackground() throws Exception {
        CuratorFramework client = getZkCurator();
        client.start();
        String path = "/zk-book";
        logger.info("Main thread: " + Thread.currentThread().getName());
        CountDownLatch countDownLatch = new CountDownLatch(2);
        ExecutorService tp = new ThreadPoolExecutor(2, 2,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());

        // 此处传入了自定义的Executor
        client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .inBackground(new BackgroundCallback() {
                    @Override
                    public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                        logger.info("event[code: " + event.getResultCode() + ", type: " + event.getType() + "]");
                        logger.info("Thread of processResult: " + Thread.currentThread().getName());
                        countDownLatch.countDown();
                    }
                }, tp)
                .forPath(path, "init".getBytes());

        // 此处没有传入自定义的Executor
        client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .inBackground(new BackgroundCallback() {
                    @Override
                    public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                        logger.info("event[code: " + event.getResultCode() + ", type: " + event.getType() + "]");
                        logger.info("Thread of processResult: " + Thread.currentThread().getName());
                        countDownLatch.countDown();
                    }
                })
                .forPath(path, "init".getBytes());

        countDownLatch.await();
        tp.shutdown();
        sleep();
    }
}
